<!--
Webug, FirePHP extension for Google Chrome
Fedosov Mikhail, 2010
www.micle.info
-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
var isPoweredOn = true;

function processWfHeaders(headersRaw)
{
	var headers = headersRaw.split("\n");
	headers.sort();
	var headersWf = [];
	for (var i in headers)
	{
		var m = headers[i].match(/^X-Wf-1-(\d+)-1-(\d+): (\d+)\|(.*)\|/i);
		if (m)
		{
			/*
			* m:
			* 1 - группа
			* 2 - id в группе
			* 3 - длинна данных
			* 4 - данные
			*/
			headersWf.push([m[1], m[2], m[3], eval(m[4])]);
		}
	}
	return headersWf;
}

function getNotificationURL(item) 
{
	var query = "";
	for (var key in item) 
	{
		var value = item[key];
		if (value) query += key + "=" + encodeURIComponent(value) + "&";
	}

	var notifyPageURL = chrome.extension.getURL("notify.html") + "?" + query;
	return notifyPageURL;
}

chrome.browserAction.onClicked.addListener(function(tab)
{
	chrome.browserAction.setBadgeText({'text': ''});
	isPoweredOn = !isPoweredOn;
	if (isPoweredOn)
	{
		chrome.browserAction.setIcon({ path: "i/FirePHP_19.png" });
	}
	else
	{
		chrome.browserAction.setIcon({ path: "i/FirePHP_19_gray.png" });
	}
});

chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo)
{
	chrome.browserAction.setBadgeText({'text': ''});
});

chrome.tabs.onUpdated.addListener(function(tabId, change, tab) 
{
	if (isPoweredOn && !(tab.url.match("chrome://") || tab.url.match("chrome-extension://") || tab.url.match("file://") || tab.url.match("about:"))) 
	{
		chrome.browserAction.setBadgeText({'text': '...'});
		if (change.status == "complete") 
		{
			var xhr = new XMLHttpRequest();
			xhr.open("HEAD", tab.url, true);
			xhr.setRequestHeader("X-FirePHP", "0.4.4");
			xhr.setRequestHeader("X-FirePHP-Version", "0.4.4");
			xhr.send();
			xhr.onreadystatechange = function()
			{
				if (xhr.readyState == 4) 
				{
					if (xhr.getAllResponseHeaders()) 
					{
						var headers = xhr.getAllResponseHeaders();
						var headersWf = processWfHeaders(headers);
						if (headersWf.length) 
						{
							chrome.browserAction.setBadgeText({'text': headersWf.length.toString()});
							var headersWfText = '';
							for (var i in headersWf) 
							{
								var type = headersWf[i][3][0].Type;
								var data = headersWf[i][3][1];
								var text = '';
								if (typeof(data) != 'object')
								{
									text = data;
									switch (type)
									{
										case 'WARN': 
											chrome.tabs.executeScript(null, { code: "console.warn('"+text+"');" });
										break;
	
										case 'ERROR': 
											chrome.tabs.executeScript(null, { code: "console.error('"+text+"');" });
										break;
	
										case 'INFO': 
											chrome.tabs.executeScript(null, { code: "console.info('"+text+"');" });
										break;
	
										default: 
											chrome.tabs.executeScript(null, { code: "console.log('"+text+"');" });
										break;
									}
								}
								else
								{
									console.log(headersWf[i]);
									chrome.tabs.executeScript(null, { code: "console.log('[');" });
									for (var ii in data)
									{
										//text = "'" + ii + "' => '" + data[ii] + "'";
										text = '   ' + ii + ' => "' + data[ii] + '"';
										chrome.tabs.executeScript(null, { code: "console.log('"+text+"');" });
									}
									chrome.tabs.executeScript(null, { code: "console.log(']');" });
								}
							}
							/*
							var notification = webkitNotifications.createHTMLNotification(getNotificationURL([tab.url, headersWfText]));
							notification.show();
							*/
							//setTimeout(function(){ notification.cancel() }, 7000);
							//console.log(text);
							//chrome.extension.getBackgroundPage().BackGroundManager.writeToConsole(someText);
							//chrome.windows.getCurrent().console.log('iok');
							//tab.console.log('iok');
							//chrome.extension.getBackgroundPage().console.log('iok');
						}
						else 
						{
							chrome.browserAction.setBadgeText({'text': ''});
						}
					}
					else 
					{
						console.error("Can't get headers.");
					}
				}
			}
		}
	}
});

// chrome.browserAction.setBadgeBackgroundColor({'color': [255, 0, 0, 255]});
</script>